﻿// 
// $Id: ForestStoreModel.cs 3981 2010-11-02 11:12:16Z unknown $
// 
// Zen UI Toolkit Copyright (c) 2010, Paul Clancy.  All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are permitted provided 
// that the following conditions are met:
// 
// * 	Redistributions of source code must retain the above copyright notice, this list of 
// 	conditions and the following disclaimer.
// 
// * 	Redistributions in binary form must reproduce the above copyright notice, this list of 
// 	conditions and the following disclaimer in the documentation and/or other materials 
// 	provided with the distribution.
// 
// * 	Neither the name of AppTek nor the names of its contributors may be used to endorse 
// 	or promote products derived from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
// THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 

#region

using System.ComponentModel;
using System.Web.UI;

#endregion

namespace Zen.Toolkit
{
    [DojoWidget(ClassMapping = "dijit.tree.ForestStoreModel", InvokeStartup = false)]
    //[DojoRequires(ModuleName = "dojo.data.ForestStoreModel")]
    public class ForestStoreModel : WidgetBase, IModel
    {
        public ForestStoreModel() : base(HtmlTextWriterTag.Div)
        {
        }

        #region IModel Members

        [DefaultValue(null)]
        public string StoreId { get; set; }

        [DojoProperty(Name = "rootId")]
        [DefaultValue("root")]
        public string RootId
        {
            get { return (string) ViewState["RouteId"] ?? ""; }
            set { ViewState["RouteId"] = value; }
        }

        [DojoProperty(Name = "rootLabel")]
        [DefaultValue("")]
        public string RootLabel
        {
            get { return (string) ViewState["RootLabel"] ?? ""; }
            set { ViewState["RootLabel"] = value; }
        }

        [DojoProperty(Name = "query", SerializeAs = SerializationType.Raw)]
        [DefaultValue("")]
        public string Query
        {
            get { return (string.Format("{{{0}}}", ViewState["Query"] ?? "")); }
            set { ViewState["Query"] = value; }
        }

        [DojoProperty(Name = "childrenAttrs")]
        [DefaultValue("")]
        public string Children
        {
            get { return (string)ViewState["Children"] ?? ""; }
            set { ViewState["Children"] = value; }
        }




        #endregion

        public override void OnBeforeGenerateScript()
        {
            if (!string.IsNullOrEmpty(StoreId))
            {
                Control control = Z.FindControl(this, StoreId);
                if (control == null)
                    throw new ZenControlNotFoundException(
                        string.Format("Could not find the store '{0}' for the model '{1}'", StoreId, ClientID));

                // now get the provider
                var dataStore = control as IDataStore;

                if (dataStore == null)
                    throw new ZenControlNotFoundException(
                        string.Format(
                            "The StoreId property of the model must be set to a control of type DataStore.  {0} is a {1}.",
                            StoreId, control.GetType().Name));


                if (dataStore.Provider.Count < 1)
                    throw new ZenConfigurationException("The store must have at least one provider");

                AddConfig("store", dataStore.Provider[0].DojoID);
            }
        }
    }
}